package org.example.ims.Controller;


import org.example.ims.DTO.Response;
import org.example.ims.Entity.User.BackupHistory;
import org.example.ims.Service.User.DatabaseBackupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/backup")
public class BackupController {
    @Autowired
    private DatabaseBackupService backupService;

    @PostMapping
    public String backup() {
        try {
            String backupFile = backupService.backupDatabase();
            return "备份成功：" + backupFile;
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return "备份失败：" + e.getMessage();
        }
    }

    @PostMapping("/restore")
    public String restore(@RequestParam String backupFile) {
        try {
            backupService.restoreDatabase(backupFile);
            return "恢复成功";
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return "恢复失败：" + e.getMessage();
        }
    }

    @GetMapping("/history")
    public Response<List<BackupHistory>> getBackupHistory() {
        try {
            List<BackupHistory> history = backupService.getRecentOperations();
            return Response.success( history,"查询成功");
        } catch (Exception e) {
            return Response.error("500","查询失败：" + e.getMessage());
        }
    }
}